<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="过期时间" prop="expireTime">
        <el-date-picker
          v-model="exdateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="类  型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择代金券类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否使用" prop="isUsed">
        <el-select v-model="queryParams.isUsed" placeholder="是否使用" clearable size="small">
          <el-option
            v-for="dict in isUsedOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="来  源" prop="couponFrom">
        <el-select v-model="queryParams.couponFrom" placeholder="请输入优惠券来源" clearable size="small">
          <el-option
            v-for="dict in couponFromOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发放时间" prop="gmtTime">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['member:coupon:add']"
        >赠送
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="couponList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="代金券金额" align="center" prop="amount"/>
      <el-table-column label="代金券类型" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column label="是否已使用" align="center" prop="isUsed" :formatter="isUsedFormat"/>
      <el-table-column label="优惠券来源" align="center" prop="couponFrom" :formatter="couponFromFormat"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="发放时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过期时间" align="center" prop="expireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogdrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型">
          <el-select v-model="form.type" placeholder="请选择代金券类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input-number v-model="form.amount" :precision="2" :step="1" :min="1" :max="300" placeholder="请输入代金券金额"></el-input-number>
        </el-form-item>
        <el-form-item label="过期时间" prop="expireTime">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.expireTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择过期时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listCoupon,  addCoupon, updateCoupon} from "@/api/member/coupon";

  export default {
    name: "Coupon",
    props: {
      memberId: {
        type: Number
      }
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 优惠券表格数据
        couponList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 代金券类型字典
        typeOptions: [],
        // 是否已使用字典
        isUsedOptions: [],
        // 优惠券来源字典
        couponFromOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          memberId: this.memberId,
          expireTime: undefined,
          amount: undefined,
          type: undefined,
          isUsed: undefined,
          couponFrom: undefined,
          gmtTime: undefined,
          exStartTime: undefined,
          exEndTime: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        // 日期范围
        dateRange: [],
        exdateRange: [],
      };
    },
    created() {
      this.getList();
      this.getDicts("coupon_type").then(response => {
        this.typeOptions = response.data;
      });
      this.getDicts("sys_is_enable").then(response => {
        this.isUsedOptions = response.data;
      });
      this.getDicts("coupon_from").then(response => {
        this.couponFromOptions = response.data;
      });
    },
    methods: {
      /** 查询优惠券列表 */
      getList() {
        this.loading = true;
        if (null != this.exdateRange && '' != this.exdateRange) {
          this.queryParams.exStartTime = this.exdateRange[0];
          this.queryParams.exEndTime = this.exdateRange[1];
        }
        listCoupon(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.couponList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 代金券类型字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.type);
      },
      // 是否已使用字典翻译
      isUsedFormat(row, column) {
        return this.selectDictLabel(this.isUsedOptions, row.isUsed);
      },
      // 优惠券来源字典翻译
      couponFromFormat(row, column) {
        return this.selectDictLabel(this.couponFromOptions, row.couponFrom);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          memberId: this.memberId,
          expireTime: undefined,
          amount: undefined,
          type: undefined,
          isUsed: undefined,
          couponFrom: undefined,
          remark: undefined,
          gmtTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "赠送代金券";
      },

      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateCoupon(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addCoupon(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      }
    }
  };
</script>
